import 'package:cbec_app/components/base/base_ctr.dart';
import 'package:cbec_app/components/http/network.dart';
import 'package:cbec_app/routes/routes.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

// 
class WithDrawController extends BaseController  with SingleGetTickerProviderMixin{

  late TabController tabController;

  final TextEditingController textCtr = TextEditingController();

  var ableClick = false.obs;


  var buttonColors = Tools.fromHex("#F58E84").obs;

  //可提现金额
  double amount = 0.00;

  //待结算金额
  double frozenAmount = 0.00;

  //当前页码
  int page = 1;

  //是否有下一页
  bool hasNextPage = true;

  //余额明细类型 null 全部 0 收入 1 支出
  dynamic type = null;

  //我的银行卡列表
  List bankList = [];

  //余额明细列表
  List withdrawLogList = [];

  //提现记录列表
  List withdrawList = [];

  //当前选择的提现银行卡
  int currentBank = 0;

  //页面 1、余额明细 2、提现记录
  int pageType = 1;


  @override
  void onInit() {
    getWithdraw();
    getMybankCardList();

    tabController = TabController(length: 3, vsync: this);
    tabController.addListener(() { 
      if (tabController.indexIsChanging) {
        // 当tab切换时执行的操作
        print("切换了状态，当前为 index: ${tabController.index}");
        switch(tabController.index){
          case 0:type=null;break;
          case 1:type=0;break;
          case 2:type=1;break;
          default:
        }
        page = 1;
        getWithdrawLog();
      }
    });

    super.onInit();
  }


  //获取可提现配置
  Future getConfig() async {
    Tools.showLoading();
    return Network.getConfig().then((value) {
      print(value);
      Tools.hideLoading();
    }).catchError((onError) {
      print(onError);
      Tools.hideLoading();
    });
  }

  //获取可提现金额
  Future getWithdraw() async {
    Tools.showLoading();
    return Network.getWithdraw().then((value) {
      amount = value['amount'];
      frozenAmount = value['frozenAmount'];
      update();
      Tools.hideLoading();
    }).catchError((onError) {
      print(onError);
      Tools.hideLoading();
    });
  }

  //前往余额明细页面
  void toSurplusLog(){
    pageType = 1;
    page = 1;//初始化页码
    getWithdrawLog();
    Get.toNamed(Routes.surplusDetail);
  }

  //前往提现记录页面
  void toWithdrawList(){
    pageType = 2;
    page = 1;//初始化页码
    getWithdrawList();
    // update();
    Get.toNamed(Routes.widthdrawLog);
  }

  //余额明细
  Future getWithdrawLog() async {
    Tools.showLoading();
    return Network.getWithdrawLog(type,page,10).then((value) {
      print(value);
      hasNextPage = value['hasNextPage'];
      if(value["hasNextPage"]){
        withdrawLogList.addAll(value['list']);
      }else{
        withdrawLogList = value['list'];
      }
      update();
      Tools.hideLoading();
    }).catchError((onError) {
      print(onError);
      Tools.hideLoading();
    });
  }

  //提现记录
  Future getWithdrawList() async {
    Tools.showLoading();
    return Network.getwithdrawList(page,10).then((value) {
      print(value);
      hasNextPage = value['hasNextPage'];
      if(page==1){
        withdrawList = value['list'];
      }else{
        withdrawList.addAll(value['list']);
      }
      update();
      Tools.hideLoading();
    }).catchError((onError) {
      print(onError);
      Tools.hideLoading();
    });
  }


  //申请提现
  Future applyWithdraw(double amount,String bankId) async {
    Tools.showLoading();
    return Network.applyWithdraw(amount,bankId).then((value) {
      // print(value);
      if(value==true){
        Tools.showToast("提现申请已提交,请耐心等待审批");
        Get.back();
        getWithdraw();
      }
      Tools.hideLoading();
    }).catchError((onError) {
      print("发生了错误:$onError");
      Tools.hideLoading();
    });
  }

  //获取我的银行卡列表
  Future getMybankCardList() async {
    Tools.showLoading();
    return Network.getMyBankCardList().then((value) {
      print(value);
      bankList = value;
      update();
      Tools.hideLoading();
    }).catchError((onError) {
      print(onError);
      Tools.hideLoading();
    });
  }
  
  //选择银行卡
  void choiseBank(int index){
    currentBank = index;
    update();
  }


  /**
   * 监听用户输入，金额检测是否可提交
   */
  void onUserInputChanged(String input) {
    if (input != '') {
      //如果输入的金额超过可提现金额就自动将其设置为最大可提现金额
      double inputAmount = double.parse(input);
      // if (amount < inputAmount) {
      //   textCtr.text = amount.toString();
      // }
      //有输入并且大于0就可提交
      if (inputAmount > 0) {
        ableClick.value = true;
        buttonColors.value = const Color.fromRGBO(248, 63, 45, 1);
      }
    } else {
      ableClick.value = false;
      buttonColors.value = Tools.fromHex("#F58E84");
    }
    update();
  }

  /**
   * 全部提现
   */
  withDrawAll() {
    if (amount > 0) {
      textCtr.text = amount.toString();
      buttonColors.value = const Color.fromRGBO(248, 63, 45, 1);
      update();
    }
  }

  /**
   * 提交表单到提现接口
   */
  submit() {
    if (textCtr.text != '') {
      double amount = double.parse(textCtr.text);
      applyWithdraw(amount, bankList[currentBank]['id']);
    } else {
      return null;
    }
  }


  @override
  void onPageRefresh() async {
    super.onPageRefresh();
    if(pageType==1){
      await getWithdrawLog();
    }else{
      await getWithdrawList();
    }
    baseRefreshCtr.refreshCompleted(resetFooterState: true);
  }

  @override
  void onPageLoading()async {
    super.onPageLoading();
    if(hasNextPage){
      page+=1;
      if(pageType==1){
        await getWithdrawLog();
      }else{
        await getWithdrawList();
      }
      baseRefreshCtr.loadComplete();
    }
  }

  @override
  void onClose() {
    tabController.dispose();
    super.onClose();
  }

}
